/*
 * Copyright (c) 2010 Sun Microsystems, Inc.
 * Copyright (c) 2011 Oracle Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

package org.sunspotworld.demo;

import com.sun.spot.core.resources.Resources;
import com.sun.spot.core.resources.transducers.ISwitchListener;
import com.sun.spot.core.resources.transducers.ITriColorLEDArray;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.core.resources.transducers.SwitchEvent;
//import com.sun.spot.util.IEEEAddress;
import java.util.Random;

/**
 *
 * @author vgupta
 */
public class SwitchListener implements ISwitchListener {
    Random rand = new Random();
    private ITriColorLEDArray myLEDs = null;
    private int idx = 0;
    private LEDColor[] ledcolor = {
            LEDColor.RED,
            LEDColor.WHITE,
            LEDColor.BLUE,
            LEDColor.GREEN,
            LEDColor.CYAN,
            LEDColor.YELLOW,
            LEDColor.MAGENTA
    };

    public SwitchListener() {
        System.out.println("Creating new switchlistener ...");
        myLEDs = (ITriColorLEDArray)
            Resources.lookup(ITriColorLEDArray.class);
    }

    
//    private HttpResponse sendCheckIn(String connStr) throws IOException {
//        // Create a registration request ...
//        HttpRequest req = new HttpRequest();
//        HttpClient httpClient = new HttpClient(connStr,
//                WoTConstants.USES_COMPRESSION);
//
//        long ourAddr = RadioFactory.getRadioPolicyManager().getIEEEAddress();
//        String name = WoTConstants.SPOT_PREFIX +
//                IEEEAddress.toDottedHex(ourAddr).substring(15);
//
//        req.setMethod("POST");
//        req.setPathInfo("/ci");
////        req.setHeader("Name", name);
////        req.setHeader("Awake-Time", "" + 10);
////        req.setHeader("Sleep-Time", "" + 60);
//        String reqBody = "name " + name + "\n" + "awake-time " + 10 + "\n" +
//                "sleep-time " + 60 + "\n";
//        req.setBody(reqBody.getBytes());
//
//        System.out.println("Sent check-in request: " + req);
//        HttpResponse res = httpClient.sendData(req);
//        System.out.println("Got check-in response: " + res);
//        return res;
//    }
//
//    public void switchPressed(SwitchEvent evt) {
//        System.out.println("Switch pressed, will send check-in message");
//        String gwAddr = DiscoveryHandler.getCurrentGatewayAddr();
//
//        if (gwAddr.equalsIgnoreCase("")) {
//            System.out.println("No current gateway!!!");
//            return;
//        }
//
//        try {
//            System.out.println("Sending check-in to " +
//                    DiscoveryHandler.getCurrentGatewayAddr());
//            HttpResponse resp =
//                    sendCheckIn(DiscoveryHandler.getCurrentGatewayAddr());
//            System.out.println("Check-in response is: " +
//                    resp.getStatus() + "(" +
//                    HttpResponse.getStatusString(resp.getStatus()) + ")");
//        } catch (IOException ioe) {
//            ioe.printStackTrace();
//        }
//    }

    public void switchPressed(SwitchEvent evt) {
        idx = (idx + 1) % (ledcolor.length + 1);
        
        for (int i = 0; i < myLEDs.size(); i++) {
            if (idx >  0) {
                myLEDs.getLED(i).setColor(ledcolor[idx - 1]);
                myLEDs.getLED(i).setOn();
            } else {
                myLEDs.getLED(i).setRGB(0, 0, 0);
                myLEDs.getLED(i).setOff();
            }
        }
    }

    public void switchReleased(SwitchEvent evt) {
    }
}
